using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace Minesweeper02
{
	/// <summary>
	/// Summary description for MainForm.
	/// </summary>
	//public class FormArgs:EventArgs{

	//}
	public class MainForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// child form
		/// </summary>
		/// 
		private BestTime besttime;
		private NewBestTime newbesttime;
		private Custom customform;
		private ContentsHelp contentsform;
		private Aboutus aboutusform;
		public bool IsContinue;
		//
		//control
		//
		private string level;
		private int columns;
		private int rows;
		private int itemWidth;
		private int itemHeight;
		private int totalMine;
		private bool mark;
		private int bestTimeLevel;
		private string playerLevel;
		private MathBase mb;
		private ImgMouseEventHandler ime;
		private GraphicsItem mf;
		private GraphicsItem numberform;
		private Image[] arrayMineStatus,arrayNumber,arrayFace;
		private int time;
		private bool enabletime;
		private bool gamestart;
		//
		//interface
		//
		private System.Windows.Forms.PictureBox left_top_conner;
		private System.Windows.Forms.PictureBox top_bg;
		private System.Windows.Forms.PictureBox right_top_conner;
		private System.Windows.Forms.PictureBox left_bg_1;
		private System.Windows.Forms.PictureBox bg_1;
		private System.Windows.Forms.PictureBox right_bg_1;
		private System.Windows.Forms.PictureBox middle_left_conner;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox middle_right_conner;
		private System.Windows.Forms.PictureBox left_bg_2;
		private System.Windows.Forms.PictureBox right_bg_2;
		private System.Windows.Forms.PictureBox left_bottom_conner;
		private System.Windows.Forms.PictureBox bottom_bg;
		private System.Windows.Forms.PictureBox right_bottom_conner;
		private System.Windows.Forms.PictureBox face;
		private System.Windows.Forms.PictureBox remainmine;
		private System.Windows.Forms.PictureBox clock;
		private System.Windows.Forms.PictureBox mineform;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem filemenu;
		private System.Windows.Forms.MenuItem beginnermenu;
		private System.Windows.Forms.MenuItem newmenu;
		private System.Windows.Forms.MenuItem space0;
		private System.Windows.Forms.MenuItem intermediatemenu;
		private System.Windows.Forms.MenuItem expertmenu;
		private System.Windows.Forms.MenuItem custommenu;
		private System.Windows.Forms.MenuItem space1;
		private System.Windows.Forms.MenuItem markmenu;
		private System.Windows.Forms.MenuItem space2;
		private System.Windows.Forms.MenuItem besttimemenu;
		private System.Windows.Forms.MenuItem space3;
		private System.Windows.Forms.MenuItem exitmenu;
		private System.Windows.Forms.MenuItem helpmenu;
		private System.Windows.Forms.MenuItem contentmenu;
		private System.Windows.Forms.MenuItem aboutmenu;
		private System.Windows.Forms.MenuItem space4;
		private System.Windows.Forms.Timer timer;
		private Image firstimgremainmine;
		private System.ComponentModel.IContainer components;
		private void InitData()
		{
			MineResourceSet rs=new MineResourceSet("Play.resources");
			string[] str=rs.LoadAttributeStartGame();
			level=str[0];
			columns=int.Parse(str[1]);
			rows=int.Parse(str[2]);
			totalMine=int.Parse(str[3]);
			if(int.Parse(str[4])==1)
			{
				mark=true;
			}
			else
			{
				mark=false;
			}
			if (level != "Custom")
			{
				bestTimeLevel=int.Parse(str[5]);
				playerLevel=str[6];
			}
			else{
				bestTimeLevel=-1;
				playerLevel="";
			}
		}
		#region procedure contain the main method to play game
		private void DrawGraphics()
		{
			DrawQueue dq;
			while (this.mb.ThisQueue.Count!=0)
			{
				dq=(DrawQueue)(this.mb.ThisQueue.Dequeue());
				this.DrawItem(dq);
			}
		}
		private void DrawItem(DrawQueue _dq)
		{
		    //process change image of item
			this.mf.DrawItem(arrayMineStatus[_dq.NImg],_dq.X,_dq.Y);
		}
		private Image DrawNumber(int n)
		{
			
			int t=0;
			if(n>=0)
			{
				for (int i=0;i<3;i++)
				{
					t=n%10;
					this.numberform.DrawItem(arrayNumber[t],2-i,0);
					n=(int)(n/10);
				}
			}
			else
			{
				this.numberform.DrawItem(arrayNumber[10],0,0);
				n=(int)(Math.Abs(n));
				for (int i=0;i<2;i++)
				{
					t=n%10;
					this.numberform.DrawItem(arrayNumber[t],2-i,0);
					n=(int)(n/10);
				}
			}
			return this.numberform.Img;
		}
		private void StopGame()
		{
			this.enabletime=false;
			this.timer.Enabled=false;
			this.gamestart=false;
			if (this.mb.IsWin)
			{
				this.remainmine.Image=this.DrawNumber(this.mb.RemainMine);
				this.face.Image=this.arrayFace[4];
				if (this.time<this.bestTimeLevel){
					MineResourceSet rs=new MineResourceSet("Play.resources");
					this.useNewBestTime();
				}
                
			}
			else
			{
				this.face.Image=this.arrayFace[3];
			}
		}
		private void StartNewGame()
		{
			this.enabletime=false;
			this.time=0;
			this.InitData();
			this.mb.RestartAttributeGame();
			this.mf.ResetBeginImage(arrayMineStatus[9]);
			this.mineform.Image=this.mf.Img;
			this.ime.ResetAttribute();
			this.clock.Image=this.DrawNumber(time);
			this.remainmine.Image=this.DrawNumber(totalMine);
		}
		private void ResetAtrribute(string _level,int _columns,int _rows,int _totalMine)
		{
			//appear when level change click
			MineResourceSet rs=new MineResourceSet("Play.resources");
			rs.SaveAttribute(_level,_columns,_rows,_totalMine,mark);
			rs.SetIsContinue(true);
			this.Close();
		}
		private void SaveAttribute()
		{
			MineResourceSet rs=new MineResourceSet("Minesweeper.resouces");
			rs.SaveAttribute(level,columns,rows,totalMine,mark);
		}
		#endregion
		#region usechildform method to display child form
		private void useBestTime(){
            this.besttime=new BestTime();
			this.besttime.ShowDialog();
			this.besttime.Dispose();
			MineResourceSet rs=new MineResourceSet("Play.resources");
			string[] str=rs.LoadBestTime();
			if (this.level=="Beginner")
			{
				this.bestTimeLevel=int.Parse(str[0]);
				this.playerLevel=str[1];
			}
			if (this.level=="Intermediate")
			{
				this.bestTimeLevel=int.Parse(str[2]);
				this.playerLevel=str[3];
			}
			if (this.level=="Expert")
			{
				this.bestTimeLevel=int.Parse(str[4]);
				this.playerLevel=str[5];
			}

		}
		private void useNewBestTime(){
			newbesttime=new NewBestTime(this.level,this.time,this.playerLevel);
			newbesttime.ShowDialog();
			newbesttime.Dispose();
			this.useBestTime();
		}
		#endregion
		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			this.IsContinue=true;
			InitData();
			MineResourceSet rs=new MineResourceSet("Minesweeper.resources");
			arrayMineStatus=rs.GetMineStatus();
			arrayNumber=rs.GetNumber();
			arrayFace=rs.GetFace();
			time=0;
			gamestart=true;
			enabletime=false;
            itemHeight=16;
			itemWidth=16;
			ime=new ImgMouseEventHandler(columns,rows,itemWidth,itemHeight);
			mb=new MathBase(columns,rows,totalMine);
			mb.Mark=mark;
			mf=new GraphicsItem(arrayMineStatus[9],columns,rows,itemWidth,itemHeight);
			numberform=new GraphicsItem(arrayNumber[0],3,1,13,23);
			firstimgremainmine=this.DrawNumber(totalMine);
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			int width=itemWidth*columns;
			int height=itemHeight*rows;
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.left_top_conner = new System.Windows.Forms.PictureBox();
			this.top_bg = new System.Windows.Forms.PictureBox();
			this.right_top_conner = new System.Windows.Forms.PictureBox();
			this.left_bg_1 = new System.Windows.Forms.PictureBox();
			this.bg_1 = new System.Windows.Forms.PictureBox();
			this.right_bg_1 = new System.Windows.Forms.PictureBox();
			this.middle_left_conner = new System.Windows.Forms.PictureBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.middle_right_conner = new System.Windows.Forms.PictureBox();
			this.left_bg_2 = new System.Windows.Forms.PictureBox();
			this.right_bg_2 = new System.Windows.Forms.PictureBox();
			this.left_bottom_conner = new System.Windows.Forms.PictureBox();
			this.bottom_bg = new System.Windows.Forms.PictureBox();
			this.right_bottom_conner = new System.Windows.Forms.PictureBox();
			this.face = new System.Windows.Forms.PictureBox();
			this.remainmine = new System.Windows.Forms.PictureBox();
			this.clock = new System.Windows.Forms.PictureBox();
			this.mineform = new System.Windows.Forms.PictureBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.filemenu = new System.Windows.Forms.MenuItem();
			this.beginnermenu = new System.Windows.Forms.MenuItem();
			this.newmenu = new System.Windows.Forms.MenuItem();
			this.space0 = new System.Windows.Forms.MenuItem();
			this.intermediatemenu = new System.Windows.Forms.MenuItem();
			this.expertmenu = new System.Windows.Forms.MenuItem();
			this.custommenu = new System.Windows.Forms.MenuItem();
			this.space1 = new System.Windows.Forms.MenuItem();
			this.markmenu = new System.Windows.Forms.MenuItem();
			this.space2 = new System.Windows.Forms.MenuItem();
			this.besttimemenu = new System.Windows.Forms.MenuItem();
			this.space3 = new System.Windows.Forms.MenuItem();
			this.exitmenu = new System.Windows.Forms.MenuItem();
			this.helpmenu = new System.Windows.Forms.MenuItem();
			this.contentmenu = new System.Windows.Forms.MenuItem();
			this.aboutmenu = new System.Windows.Forms.MenuItem();
			this.space4 = new System.Windows.Forms.MenuItem();
			this.timer = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// left_top_conner
			// 
			this.left_top_conner.Image = ((System.Drawing.Image)(resources.GetObject("left_top_conner.Image")));
			this.left_top_conner.Location = new System.Drawing.Point(0, 0);
			this.left_top_conner.Name = "left_top_conner";
			this.left_top_conner.Size = new System.Drawing.Size(11, 12);
			this.left_top_conner.TabIndex = 0;
			this.left_top_conner.TabStop = false;
			// 
			// top_bg
			// 
			this.top_bg.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("top_bg.BackgroundImage")));
			this.top_bg.Location = new System.Drawing.Point(11, 0);
			this.top_bg.Name = "top_bg";
			this.top_bg.Size = new System.Drawing.Size(width, 12);
			this.top_bg.TabIndex = 1;
			this.top_bg.TabStop = false;
			// 
			// right_top_conner
			// 
			this.right_top_conner.Image = ((System.Drawing.Image)(resources.GetObject("right_top_conner.Image")));
			this.right_top_conner.Location = new System.Drawing.Point(width+11, 0);
			this.right_top_conner.Name = "right_top_conner";
			this.right_top_conner.Size = new System.Drawing.Size(7, 12);
			this.right_top_conner.TabIndex = 2;
			this.right_top_conner.TabStop = false;
			// 
			// left_bg_1
			// 
			this.left_bg_1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("left_bg_1.BackgroundImage")));
			this.left_bg_1.Location = new System.Drawing.Point(0, 12);
			this.left_bg_1.Name = "left_bg_1";
			this.left_bg_1.Size = new System.Drawing.Size(11, 36);
			this.left_bg_1.TabIndex = 3;
			this.left_bg_1.TabStop = false;
			// 
			// bg_1
			// 
			this.bg_1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("bg_1.BackgroundImage")));
			this.bg_1.Location = new System.Drawing.Point(11, 12);
			this.bg_1.Name = "bg_1";
			this.bg_1.Size = new System.Drawing.Size(width, 36);
			this.bg_1.TabIndex = 4;
			this.bg_1.TabStop = false;
			// 
			// right_bg_1
			// 
			this.right_bg_1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("right_bg_1.BackgroundImage")));
			this.right_bg_1.Location = new System.Drawing.Point(width+11, 12);
			this.right_bg_1.Name = "right_bg_1";
			this.right_bg_1.Size = new System.Drawing.Size(7, 36);
			this.right_bg_1.TabIndex = 5;
			this.right_bg_1.TabStop = false;
			// 
			// middle_left_conner
			// 
			this.middle_left_conner.Image = ((System.Drawing.Image)(resources.GetObject("middle_left_conner.Image")));
			this.middle_left_conner.Location = new System.Drawing.Point(0, 48);
			this.middle_left_conner.Name = "middle_left_conner";
			this.middle_left_conner.Size = new System.Drawing.Size(11, 11);
			this.middle_left_conner.TabIndex = 6;
			this.middle_left_conner.TabStop = false;
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("pictureBox1.BackgroundImage")));
			this.pictureBox1.Location = new System.Drawing.Point(11, 48);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(width, 11);
			this.pictureBox1.TabIndex = 7;
			this.pictureBox1.TabStop = false;
			// 
			// middle_right_conner
			// 
			this.middle_right_conner.Image = ((System.Drawing.Image)(resources.GetObject("middle_right_conner.Image")));
			this.middle_right_conner.Location = new System.Drawing.Point(width+11, 48);
			this.middle_right_conner.Name = "middle_right_conner";
			this.middle_right_conner.Size = new System.Drawing.Size(7, 11);
			this.middle_right_conner.TabIndex = 8;
			this.middle_right_conner.TabStop = false;
			// 
			// left_bg_2
			// 
			this.left_bg_2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("left_bg_2.BackgroundImage")));
			this.left_bg_2.Location = new System.Drawing.Point(0, 59);
			this.left_bg_2.Name = "left_bg_2";
			this.left_bg_2.Size = new System.Drawing.Size(11, height);
			this.left_bg_2.TabIndex = 9;
			this.left_bg_2.TabStop = false;
			// 
			// right_bg_2
			// 
			this.right_bg_2.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("right_bg_2.BackgroundImage")));
			this.right_bg_2.Location = new System.Drawing.Point(width+11, 59);
			this.right_bg_2.Name = "right_bg_2";
			this.right_bg_2.Size = new System.Drawing.Size(7, height);
			this.right_bg_2.TabIndex = 10;
			this.right_bg_2.TabStop = false;
			// 
			// left_bottom_conner
			// 
			this.left_bottom_conner.Image = ((System.Drawing.Image)(resources.GetObject("left_bottom_conner.Image")));
			this.left_bottom_conner.Location = new System.Drawing.Point(0, height+59);
			this.left_bottom_conner.Name = "left_bottom_conner";
			this.left_bottom_conner.Size = new System.Drawing.Size(12, 8);
			this.left_bottom_conner.TabIndex = 11;
			this.left_bottom_conner.TabStop = false;
			// 
			// bottom_bg
			// 
			this.bottom_bg.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("bottom_bg.BackgroundImage")));
			this.bottom_bg.Location = new System.Drawing.Point(12, height+59);
			this.bottom_bg.Name = "bottom_bg";
			this.bottom_bg.Size = new System.Drawing.Size(width-1, 8);
			this.bottom_bg.TabIndex = 12;
			this.bottom_bg.TabStop = false;
			// 
			// right_bottom_conner
			// 
			this.right_bottom_conner.Image = ((System.Drawing.Image)(resources.GetObject("right_bottom_conner.Image")));
			this.right_bottom_conner.Location = new System.Drawing.Point(width+11, height+59);
			this.right_bottom_conner.Name = "right_bottom_conner";
			this.right_bottom_conner.Size = new System.Drawing.Size(7, 8);
			this.right_bottom_conner.TabIndex = 13;
			this.right_bottom_conner.TabStop = false;
			// 
			// face
			// 
			this.face.Image = ((System.Drawing.Image)(resources.GetObject("face.Image")));
			this.face.Location = new System.Drawing.Point((int)(width/2-1), 18);
			this.face.Name = "face";
			this.face.Size = new System.Drawing.Size(24, 24);
			this.face.TabIndex = 14;
			this.face.TabStop = false;
			this.face.Click += new System.EventHandler(this.newmenu_Click);
			this.face.MouseUp += new System.Windows.Forms.MouseEventHandler(this.face_mouseup);
			this.face.MouseDown += new System.Windows.Forms.MouseEventHandler(this.face_mousedown);
			// 
			// remainmine
			// 
			this.remainmine.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("remainmine.BackgroundImage")));
			this.remainmine.Image = firstimgremainmine;
			this.remainmine.Location = new System.Drawing.Point(18, 18);
			this.remainmine.Name = "remainmine";
			this.remainmine.Size = new System.Drawing.Size(39, 23);
			this.remainmine.TabIndex = 15;
			this.remainmine.TabStop = false;
			// 
			// clock
			// 
			this.clock.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("clock.BackgroundImage")));
			this.clock.Image = this.DrawNumber(0);
			this.clock.Location = new System.Drawing.Point(width-35, 18);
			this.clock.Name = "clock";
			this.clock.Size = new System.Drawing.Size(39, 23);
			this.clock.TabIndex = 16;
			this.clock.TabStop = false;
			// 
			// mineform
			// 
			this.mineform.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("mineform.BackgroundImage")));
			this.mineform.Image = this.mf.Img;
			this.mineform.Location = new System.Drawing.Point(11, 59);
			this.mineform.Name = "mineform";
			this.mineform.Size = new System.Drawing.Size(width, height);
			this.mineform.TabIndex = 17;
			this.mineform.TabStop = false;
			this.mineform.MouseUp += new System.Windows.Forms.MouseEventHandler(this.mineform_mouseup);
			this.mineform.MouseMove += new System.Windows.Forms.MouseEventHandler(this.mineform_mousemove);
			this.mineform.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mineform_mousedown);
			//
			//ImgMouseEventHandler
			//
			this.ime.IEvent+=new Minesweeper02.ImgMouseEventHandler.ImgEventHandler(ime_IEvent);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.filemenu,
																					  this.helpmenu});
			// 
			// filemenu
			// 
			this.filemenu.Index = 0;
			this.filemenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.newmenu,
																					 this.space0,
																					 this.beginnermenu,
																					 this.intermediatemenu,
																					 this.expertmenu,
																					 this.custommenu,
																					 this.space1,
																					 this.markmenu,
																					 this.space2,
																					 this.besttimemenu,
																					 this.space3,
																					 this.exitmenu});
			this.filemenu.Text = "&File";
			// 
			// beginnermenu
			// 
			this.beginnermenu.Index = 2;
			this.beginnermenu.ShowShortcut = false;
			this.beginnermenu.Checked=(level=="Beginner");
			this.beginnermenu.Text = "Beginner";
			this.beginnermenu.Click += new System.EventHandler(this.beginnermenu_Click);
			// 
			// newmenu
			// 
			this.newmenu.Index = 0;
			this.newmenu.Shortcut = System.Windows.Forms.Shortcut.F2;
			this.newmenu.Text = "New";
			this.newmenu.Click += new System.EventHandler(this.newmenu_Click);
			// 
			// space0
			// 
			this.space0.Index = 1;
			this.space0.Text = "-";
			// 
			// intermediatemenu
			// 
			this.intermediatemenu.Index = 3;
			this.intermediatemenu.Text = "Intermediate";
			this.intermediatemenu.Checked=(this.level=="Intermediate");
			this.intermediatemenu.Click += new System.EventHandler(this.intermediatemenu_Click);
			// 
			// expertmenu
			// 
			this.expertmenu.Index = 4;
			this.expertmenu.Text = "Expert";
			this.expertmenu.Checked=(level=="Expert");
			this.expertmenu.Click += new System.EventHandler(this.expertmenu_Click);
			// 
			// custommenu
			// 
			this.custommenu.Index = 5;
			this.custommenu.Text = "Custom..";
			this.custommenu.Checked=(level=="Custom");
			this.custommenu.Click += new System.EventHandler(this.custommenu_Click);
			// 
			// space1
			// 
			this.space1.Index = 6;
			this.space1.Text = "-";
			// 
			// markmenu
			// 
			this.markmenu.Index = 7;
			this.markmenu.Checked=this.mark;
			this.markmenu.Text = "Mark(?)";
			this.markmenu.Click += new System.EventHandler(this.markmenu_Click);
			// 
			// space2
			// 
			this.space2.Index = 8;
			this.space2.Text = "-";
			// 
			// besttimemenu
			// 
			this.besttimemenu.Index = 9;
			this.besttimemenu.Text = "Best Time";
			this.besttimemenu.Click += new System.EventHandler(this.besttimemenu_Click);
			// 
			// space3
			// 
			this.space3.Index = 10;
			this.space3.Text = "-";
			// 
			// exitmenu
			// 
			this.exitmenu.Index = 11;
			this.exitmenu.Shortcut = System.Windows.Forms.Shortcut.AltF4;
			this.exitmenu.Text = "E&xit";
			this.exitmenu.Click += new System.EventHandler(this.exitmenu_Click);
			// 
			// helpmenu
			// 
			this.helpmenu.Index = 1;
			this.helpmenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.contentmenu,
																					 this.space4,
																					 this.aboutmenu});
			this.helpmenu.Text = "Help";
			// 
			// contentmenu
			// 
			this.contentmenu.Index = 0;
			this.contentmenu.Shortcut = System.Windows.Forms.Shortcut.F1;
			this.contentmenu.Text = "Contents";
			this.contentmenu.Click += new System.EventHandler(this.contentmenu_Click);
			// 
			// aboutmenu
			// 
			this.aboutmenu.Index = 2;
			this.aboutmenu.Text = "About me";
			this.aboutmenu.Click += new System.EventHandler(this.aboutmenu_Click);
			// 
			// space4
			// 
			this.space4.Index = 1;
			this.space4.Text = "-";
			// 
			// timer
			// 
			this.timer.Interval = 1000;
			this.timer.Enabled=false;
			this.timer.Tick += new System.EventHandler(this.timer_Tick);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(width+25, height+120);
			this.MaximumSize=new Size(width+25,height+120);
			this.MinimumSize=new Size(width+25,height+120);
			this.MaximizeBox=false;
			//handler
			//			this.MouseDown+=new MouseEventHandler(MainForm_MouseDown);
			//			this.MouseUp+=new MouseEventHandler(MainForm_MouseUp);
			//
			this.Controls.Add(this.mineform);
			this.Controls.Add(this.clock);
			this.Controls.Add(this.remainmine);
			this.Controls.Add(this.face);
			this.Controls.Add(this.right_bottom_conner);
			this.Controls.Add(this.bottom_bg);
			this.Controls.Add(this.left_bottom_conner);
			this.Controls.Add(this.right_bg_2);
			this.Controls.Add(this.left_bg_2);
			this.Controls.Add(this.middle_right_conner);
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.middle_left_conner);
			this.Controls.Add(this.right_bg_1);
			this.Controls.Add(this.bg_1);
			this.Controls.Add(this.left_bg_1);
			this.Controls.Add(this.right_top_conner);
			this.Controls.Add(this.top_bg);
			this.Controls.Add(this.left_top_conner);
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.Text = "Minesweeper";
			this.ResumeLayout(false);

		}
		#endregion
		static void Main()
		{
			MineResourceSet rs=new MineResourceSet("Play.resources");
			MainForm mainform=new MainForm();
			Application.Run(mainform);
			while (rs.IsContinue())
			{
				rs.SetIsContinue(false);
				mainform=new MainForm();
				Application.Run(mainform);
			}

		}
		
		private void ime_IEvent(object sender,ImgEventArgs iea)
		{
			if (gamestart)
			{
				switch(iea.Event)
				{
					case EventList.LeftDown:
						this.mb.LeftDown(iea.X,iea.Y);
						DrawGraphics();
						break;
					case EventList.BothDown:
						this.mb.BothDown(iea.X,iea.Y);
						DrawGraphics();
						break;
					case EventList.LeftMove:
						this.mb.LeftMove(iea.X,iea.Y);
						DrawGraphics();
						break;
					case EventList.BothMove:
						this.mb.BothMove(iea.X,iea.Y);
						DrawGraphics();
						break;
					case EventList.LeftOut:
						this.mb.LeftOut(iea.X,iea.Y);
						DrawGraphics();
						break;
					case EventList.BothOut:
						this.mb.BothOut(iea.X,iea.Y);
						DrawGraphics();
						break;
					case EventList.LeftClick:
						if (this.enabletime==false)
						{
							this.enabletime=true;
							this.timer.Enabled=true;
							this.mb.MakeMineMap(iea.Y*columns+iea.X);
							this.mb.LeftClick(iea.X,iea.Y);
							DrawGraphics();
						}
						else
						{
							this.mb.LeftClick(iea.X,iea.Y);
							if (this.mb.IsFinished)
							{
								this.mb.Finished();
							}
							if (this.mb.IsWin)
							{
								this.mb.Win();
							}
							DrawGraphics();
						}
						break;
					case EventList.BothClick:
						this.mb.BothClick(iea.X,iea.Y);
						if (this.mb.IsFinished)
						{
							this.mb.Finished();
						}
						if (this.mb.IsWin)
						{
							this.mb.Win();
						}
						DrawGraphics();
						break;
					case EventList.RightClick:
						this.mb.RightClick(iea.X,iea.Y);
						DrawGraphics();
						break;
					default:
						break;
				}
			}
		}
		private void newmenu_Click(object sender, System.EventArgs e)
		{
			this.gamestart=true;
			this.enabletime=false;
			this.timer.Enabled=false;
			this.mb.RestartAttributeGame();
			this.mf.ResetBeginImage(arrayMineStatus[9]);
			this.remainmine.Image=this.DrawNumber(totalMine);
			time=0;
			this.clock.Image=this.DrawNumber(time);
			this.face.Image=this.arrayFace[0];
		}

		private void beginnermenu_Click(object sender, System.EventArgs e)
		{
			MineResourceSet rs=new MineResourceSet("Minesweeper.resources");
			string[] str=rs.LoadGameLevel("Beginner");
			this.ResetAtrribute("Beginner",int.Parse(str[0]),int.Parse(str[1]),int.Parse(str[2]));
			this.beginnermenu.Checked=true;
		}

		private void intermediatemenu_Click(object sender, System.EventArgs e)
		{
			MineResourceSet rs=new MineResourceSet("Minesweeper.resources");
			string[] str=rs.LoadGameLevel("Intermediate");
			this.ResetAtrribute("Intermediate",int.Parse(str[0]),int.Parse(str[1]),int.Parse(str[2]));
			this.intermediatemenu.Checked=true;	
		}

		private void expertmenu_Click(object sender, System.EventArgs e)
		{
			MineResourceSet rs=new MineResourceSet("Minesweeper.resources");
			string[] str=rs.LoadGameLevel("Expert");
			this.ResetAtrribute("Expert",int.Parse(str[0]),int.Parse(str[1]),int.Parse(str[2]));
			this.expertmenu.Checked=true;	
		}

		private void custommenu_Click(object sender, System.EventArgs e)
		{
		//
		    this.customform=new Custom(columns,rows,totalMine,mark);
			this.customform.ShowDialog();
			this.Close();
		}

		private void markmenu_Click(object sender, System.EventArgs e)
		{
			mark=!mark;
			this.mb.Mark=mark;
			this.markmenu.Checked=mark;
		}

		private void besttimemenu_Click(object sender, System.EventArgs e)
		{
		      this.useBestTime();
		}

		private void exitmenu_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void contentmenu_Click(object sender, System.EventArgs e)
		{
		    this.contentsform=new ContentsHelp();
			Thread t=new Thread(new ThreadStart(contentsform.Screendialog));
			t.Start();
		}
		private void aboutmenu_Click(object sender, System.EventArgs e)
		{
		    this.aboutusform=new Aboutus();
			Thread t=new Thread(new ThreadStart(aboutusform.Screendialog));
			t.Start();
		}

		private void timer_Tick(object sender, System.EventArgs e)
		{
			this.time++;
			this.DrawNumber(time);
			this.clock.Image=this.numberform.Img;
		}

		private void face_mousedown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.face.Image=arrayFace[2];
		}

		private void face_mouseup(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(this.gamestart){this.face.Image=arrayFace[0];}
		}

		private void mineform_mousedown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(this.gamestart)
			{
				this.face.Image=arrayFace[1];
			}
			this.ime.MouseDown(sender,e);
			this.remainmine.Image=this.DrawNumber(this.mb.RemainMine);
			this.mineform.Image=this.mf.Img;
		}

		private void mineform_mousemove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			this.ime.MouseMove(sender,e);
            this.mineform.Image=this.mf.Img;
		}

		private void mineform_mouseup(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (this.gamestart){this.face.Image=arrayFace[0];}
			this.ime.MouseUp(sender,e);
			this.mineform.Image=this.mf.Img;
			if (this.mb.IsWin || this.mb.IsFinished)
			{
				this.StopGame();
			}
		
		}
	}
}
